﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class FinanceController : Controller
    {
        private IFinanceRepository financeRepository;
        private IUnitOfWork unitOfWork;
        public FinanceController(IFinanceRepository financeRepository,
            IUnitOfWork unitOfWork)
        {
            this.financeRepository = financeRepository;
            this.unitOfWork = unitOfWork;
        }

        //添加财务记录HttpGet页面
        [HttpGet]
        public PartialViewResult AddFinance()
        {
            return PartialView();
        }
        //添加财务记录HttpPost页面
        [HttpPost]
        public RedirectToRouteResult AddFinance(FinanceModel financeModel)
        {
            int type = financeModel.Type;
            if (ModelState.IsValid)
            {
                Finance finance = new Finance();
                finance.Income = financeModel.Income * type;
                finance.FinanceDate = financeModel.FinanceDate;
                finance.Remark = financeModel.Remark;

                financeRepository.Add(finance);
                unitOfWork.Commit();
            }

            Session["url"] = "/Finance/ManageFinance";
            return RedirectToAction("Index", "HumanResources");
        }

        //将Finance转化为FinanceModel 
        public IList<FinanceModel> FinanceToFinanceModel()
        {
            var finances = financeRepository.GetAll();
            IList<FinanceModel> financeModels = new List<FinanceModel>();
            foreach (Finance f in finances)
            {
                financeModels.Add(new FinanceModel()
                                      {
                                          Id = f.Id,
                                          Income = Math.Abs(f.Income),
                                          Type = (f.Income < 0) ? -1 : 1,
                                          FinanceDate = f.FinanceDate,
                                          Remark = f.Remark
                                      });
            }
            return financeModels;
        }
        //财务管理的HttpGet页面
        [HttpGet]
        public PartialViewResult ManageFinance()
        {
            return PartialView(FinanceToFinanceModel());
        }
        //财务管理页面的ajax
        [GridAction]
        public ActionResult ManageFinanceAjax()
        {
            return View(new GridModel(FinanceToFinanceModel()));
        }
        //删除财务记录
        public PartialViewResult DeleteFinance(int id)
        {
            Finance finance = financeRepository.GetById(id);
            if (finance != null)
            {
                financeRepository.Delete(finance);
                unitOfWork.Commit();
            }
            return PartialView("ManageFinance", FinanceToFinanceModel());
        }
        //修改财务记录
        [HttpGet]
        public ActionResult EditFinance(int id)
        {
            Finance finance = financeRepository.GetById(id);
            if (finance != null)
            {
                FinanceModel financeModel = new FinanceModel()
                                                {
                                                    Id = finance.Id,
                                                    Income = Math.Abs(finance.Income),
                                                    Type = (finance.Income < 0) ? -1 : 1,
                                                    FinanceDate = finance.FinanceDate,
                                                    Remark = finance.Remark
                                                };
                return PartialView(financeModel);
            }
            Session["url"] = "/Finance/AddFinance";
            return RedirectToAction("AddFinance");
        }
        [HttpPost]
        public ActionResult EditFinance(FinanceModel financeModel)
        {
            Finance finance = financeRepository.GetById(financeModel.Id);
            if (finance != null)
            {
                finance.Income = financeModel.Income * financeModel.Type;
                finance.FinanceDate = financeModel.FinanceDate;
                finance.Remark = financeModel.Remark;

                financeRepository.Update(finance);
                unitOfWork.Commit();
            }

            return RedirectToAction("Index","HumanResources");
        }
    }
}
